//
//  TTOAutoModel.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/22.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

class TTOAutoModel: NSObject {
    
    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userFile.swift")
    
    
    static let sharedOautoModel:TTOAutoModel = TTOAutoModel()
    
    override init() {

        super.init()
        
        self.userModel = getUserInfos()

    }
    
    var userDate : String?{
        
        if userModel?.access_token != nil {
            
            if userModel?.created_at?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                
                return userModel?.access_token
            }else{
                
                return nil
            }
            
            
        }else{
            
            return nil
        }
    
    }
    
    
    var isLogin : Bool{
        
        return  userDate != nil
    }
    
//    创建用户模型
    
    var userModel:TTNetwokingModel?
    
  
    func getUserData(code:String,isSuccess:(succsee:Bool)->()){
        
        TTNetworkingTools.sharedNetworkManager.loadDataUserAccount(code, success: { (responseObject) -> () in
            
            
            guard let obj = responseObject as? [String : AnyObject] else{
                
                isSuccess(succsee: false)
                
                return

            }
            
               let userData = TTNetwokingModel(dict: obj)
                
                self.getUserInfo(userData, idSuccess: isSuccess)
                
            
            
            }) { (error) -> () in
             
                 print("请求失败\(error)")
               isSuccess(succsee: false)
                
        }
        
    }
    
    func getUserInfo(userAccount:TTNetwokingModel,idSuccess:(succsee:Bool)->()){
        
        TTNetworkingTools.sharedNetworkManager.loadDataUserInfo(userAccount, success: { (responseObject) -> () in
            
            guard let obj = responseObject as? [String: AnyObject] else{
                
                idSuccess(succsee: false)
                
                return
            }
            
               userAccount.screen_name = obj["screen_name"] as? String
               userAccount.avatar_large = obj["avatar_large"] as? String
                
                self.saveUserInfo(userAccount)
            
            idSuccess(succsee: true)
    
               }) { (error) -> () in
                 print("请求失败\(error)")
                idSuccess(succsee: false)
        }
    }
}


extension TTOAutoModel{
    
    //    归档
    
    func saveUserInfo(userInfo:TTNetwokingModel){
        
        self.userModel = userInfo
        
        NSKeyedArchiver.archiveRootObject(userInfo, toFile: file)
    }
    
    //    解档
    
    func getUserInfos()->TTNetwokingModel?{
        
        let userInfo  = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? TTNetwokingModel
        return userInfo
    }

    
}
